package com.gdth.widget.calendar;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

/**
 * 日期数据源
 * @author lenovo
 *
 * @param <V>
 */
public class CalendarViewAdapter<V extends View> extends PagerAdapter {
	public static final String TAG = "CalendarViewAdapter";
	
	/**
	 * 数据源
	 */
	private V[] items;

	/**
	 * 构造函数
	 * @param views
	 */
	public CalendarViewAdapter(V[] items) {
		super();
		this.items = items;
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == ((View) object);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) container);
	}
	
	public V[] getAllItems() {
		return items;
	}
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		//判断是否超出数据源的长度，超出删除对应的View
		if (((ViewPager) container).getChildCount() == items.length) {
			((ViewPager) container).removeView(items[position % items.length]);
		}
		
		((ViewPager) container).addView(items[position % items.length], 0);
		return items[position % items.length];
	}

}